package homework2.NearestPoints;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * Demo for several pairs nearest points exist
 * 
 * @author eDisOn 2009212634 lilh09@mails.tsinghua.edu.cn
 */
public class SeveralPairsDemoFrame extends JInternalFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4626500656684402429L;

	private static final int CASCADE_WIDTH = 100;
	private static final int SIZE_X = 600;
	private static final int SIZE_Y = 500;
	private static final int R = 3; // radius of points

	ArrayList<Point> points = new ArrayList<Point>();
	ArrayList<NearestPoints> nearestPointsArr = new ArrayList<NearestPoints>();

	private JPanel paintPanel;
	private JPanel runPanel;
	private JButton runButton;
	CalcNearestPoints calculator;

	public SeveralPairsDemoFrame()
	{
		super("Demo for several pairs of nearest points", true, true, true,
				true);
		init();
	}

	private void init()
	{
		points.add(new Point(100, 100));
		points.add(new Point(150, 150));
		points.add(new Point(30, 150));
		points.add(new Point(300, 30));
		points.add(new Point(250, 80));

		points.add(new Point(244, 190));
		points.add(new Point(371, 130));
		points.add(new Point(471, 61));
		points.add(new Point(459, 244));
		points.add(new Point(315, 302));
		points.add(new Point(95, 332));

		points.add(new Point(161, 251));
		points.add(new Point(346, 221));
		points.add(new Point(493, 152));
		points.add(new Point(543, 202));

		paintPanel = new JPanel()
		{
			public void paint(Graphics g)
			{
				super.paint(g);
				Graphics2D g2D = (Graphics2D) g;
				g2D.setColor(Color.DARK_GRAY);
				for (Point point : points)
				{
					int x = point.x > R ? point.x - R : 0;
					int y = point.y > R ? point.y - R : 0;

					g2D.fill(new Ellipse2D.Double(x, y, 2 * R + 1, 2 * R + 1));
				}

				g.setColor(Color.BLUE);
				for (NearestPoints nearestPoints : nearestPointsArr)
				{
					g.drawLine(nearestPoints.firPoint.x,
							nearestPoints.firPoint.y, nearestPoints.secPoint.x,
							nearestPoints.secPoint.y);
				}
			}
		};
		add(paintPanel, BorderLayout.CENTER);

		runButton = new JButton("RUN");
		runButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				calculator = new CalcNearestPoints();
				nearestPointsArr = calculator.exec(points);

				JOptionPane.showMessageDialog(SeveralPairsDemoFrame.this,
						calculator.getResult(nearestPointsArr));
				paintPanel.repaint();
			}
		});

		runPanel = new JPanel();
		runPanel.setLayout(new FlowLayout());
		runPanel.setBackground(Color.LIGHT_GRAY);
		runPanel.add(runButton);

		add(runPanel, BorderLayout.SOUTH);

		setBounds(CASCADE_WIDTH, CASCADE_WIDTH, SIZE_X, SIZE_Y);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
	}
}
